<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@include file="../connection.jsp"%>

<%

            //HttpSession session = request.getSession();
            //session = request.getSession();

            String userid = (String) session.getAttribute("admin");

            if (userid == null) {
                response.sendRedirect("../login.jsp");
            }

            Connection c = getAdminConnection();
            Statement s = c.createStatement();
			Statement s2 = c.createStatement();
			
            String userids[] = request.getParameterValues("userid");
			String dbs[] = request.getParameterValues("dbs");
			
			//out.print("dbs.length"+userids.length);
            String message = "";
            if (userids != null) {
			
				for(int i=0; i<userids.length;i++){
                	s.executeUpdate("update users set db_id='"+dbs[i]+"' where userid='"+userids[i]+"'");
				}
				message="Database(s) assigned successfully.";
            }
%>

<html>
    <head>
	<link href="../css/medbill.css" rel="stylesheet" type="text/css" />
	<title>PMS Home</title>
        <script language="javascript">
            function deleteUser(user){
                if(confirm("Are you sure you want to delete this user?")){
                    document.getElementById("userid").value = user;
                    document.userForm.submit();
                }
                return false;
            }
            function updateUser(userid, status){
                location = "updateUser.jsp?status="+status+"&userid="+userid;
            }
        </script>
    </head>
    
    <body>
	<table width="860" height="100%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td height="204" valign="top"><%@include file="header.jsp"%></td>
  </tr>
  <tr>
    <td height="30" bgcolor="#1f226f" class="headingBlue">Medical Billing and Claim Processing Professionals</td>
  </tr>
  <tr>
    <td valign="top" bgcolor="#ccae74"><table width="96%" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="470" valign="top" class="txtPageTitle">Assign Databaseses</td>
            <td>&nbsp;</td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="560"></td>
              </tr>
              <tr>
                <td class="txtHomeBody"></td>
              </tr>
            </table>
              <br>        
		<form name="userForm" action="assigndbs.jsp" method="post">	
        <font color="red"><%=message%></font>
        <table border="1" width="100%" style="border-collapse: collapse" bordercolor="#C0C0C0" cellspacing="1">
            <tr>
                <th height="21">User Id</th>
                <th>User Name</th>
                <th>Type</th>                
                <th>Status</th>
				<th>Database</th>
               </tr>
            <%
            ResultSet rs = s.executeQuery("select * from users where userid !='" + userid + "' order by name");
            while (rs.next()) {
            %>
            <tr>
                <td height="21">&nbsp;<input type="hidden" value="<%=rs.getString("userid")%>" name="userid"><%=rs.getString("userid")%></td>
                <td height="21">&nbsp;<%=rs.getString("name")%></td>
                <td height="21">&nbsp;<%if (rs.getString("admin").equals("0")) {
                    out.print("Standard");
                }else{
                    out.print("Administrator");
                }%></td>
				<td height="21">&nbsp;<%if (rs.getString("active").equals("1")) {
                    out.print("Active");
                } else {
                    out.print("In-Active");
                }%></td>
                <td height="21">&nbsp;
                  <select name="dbs" size="1">
                    <option value="0">None</option>
                    <%
				ResultSet rs2 = s2.executeQuery("SELECT * FROM dbinfo");
				while(rs2.next()){
				%>
                    <option value="<%=rs2.getString("id")%>" <%if(rs2.getString("id").equals(rs.getString("db_id"))){out.print("selected");}%>><%=rs2.getString("name")%></option>
                    <%}%>
                  </select></td>
            </tr>
            <%}%>
        </table>
        
        <p>&nbsp;</p>
        <input type="submit" value="Save" />
		<input type="button" value="Cancel" onclick="location='home.jsp'"/>
          </form>
		<br /><br />
<br />


			  </td>
            <td width="45" valign="top">&nbsp;</td>
            <td valign="top">&nbsp;</td>
          </tr>
        </table></td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td height="62" valign="top"><!-- #BeginLibraryItem "/Library/footer.lbi" --><table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td height="4" bgcolor="#c7540f"></td>
      </tr>
      <tr>
        <td height="58" bgcolor="#1e2667">&nbsp;</td>
      </tr>
    </table><!-- #EndLibraryItem --></td>
  </tr>
</table>
    </body>
</html>
<%          rs.close();
            c.close();
            s.close();
%>